#!/usr/bin/env python
#
# Started on 26, Apr 2007 by Maxin B. John <maxinbjohn@gmail.com>
# This file is licensed under the MIT License.
# Command line options by gnuyoga
# python script.py --help to see the command line options
#importing the Pyosd and xml parser

import pyosd
from xml.sax import handler, make_parser
import sys, string , time
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-c","--color",dest="color",help="summary will be displayed in this color, default red")
parser.add_option("-p","--position",dest="position",help="where to place it top/bottom, default bottom")

(options,args) = parser.parse_args()

# print options.color
# print options.position

class SaxDocumentHandler(handler.ContentHandler):
    
    def __init__(self, outfile):
        self.outfile = outfile
        self.level = 0
        self.xmlcontent=""
	self.pubDate=""
	self.titles = ""
	self.description=""
	self.displayer= pyosd.osd()

    def startDocument(self):
        pass

    def endDocument(self):
        if options.color == None:
            self.displayer.set_colour("red")
        else:
            self.displayer.set_colour(options.color)
        
        self.displayer.set_timeout(5)
        if options.position == None:
            self.displayer.set_pos(1)
        elif  options.position == 'bottom':
            self.displayer.set_pos(1)
        elif  options.position == 'top':
            self.displayer.set_pos(0)
        elif  options.position == 'middle':
            self.displayer.set_pos(2)
            
            
            
        self.displayer.set_shadow_offset(2)
        self.displayer.display( self.description )

    def startElement(self, name, attrs):
        self.level += 1
        self.xmlcontent=name

    def endElement(self, name):
        pass

    def characters(self, chrs):
        if self.xmlcontent == 'pubDate':
		self.pubDate+= chrs
        if self.xmlcontent == 'title':
		self.titles+=chrs
        if self.xmlcontent == 'description':
		self.description+=chrs
		

def xmlparse(inFileName):
    outFile = sys.stdout
    # Create an instance of the Handler.
    handler = SaxDocumentHandler(outFile)
    # Create an instance of the parser.
    parser = make_parser()
    # Set the content handler.
    parser.setContentHandler(handler)
    # Parse the file
    parser.parse(inFileName)
    # sleeping for 5 seconds to prevent a sudden exit
    time.sleep(5)

def main():
    args = sys.argv[1:]
    #if len(args) != 1:
    #    print 'usage: python cricketonscreen.py  http://www.cricinfo.com/rss/livescores.xml'
    #    sys.exit(-1)
    # Here we are depending on cricinfo for the latest scores
    xmlparse("http://www.cricinfo.com/rss/livescores.xml")

#Finally triger the events

if __name__ == '__main__':
""" This main routine will call the main() and display the scores on the screen """
      while 1:
	main()
	time.sleep(5)

